<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Forpastime < forpastime@qq.com>
// +----------------------------------------------------------------------
namespace app\tenant\model;

use think\db\Query;
use think\Model;
use tree\Tree;

class TenantCategoryModel extends Model{

	/**
     * 开启时间字段自动写入
     */
    protected $autoWriteTimestamp = true;

	/**
     * 所有分类
     * @return Array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
	public function getTenantCategories()
	{
		$list = $this->where('delete_time',0)->order('list_order asc ,id asc, status desc')->select()->toArray();
		$tree       = new Tree();
        $tree->icon = ['&nbsp;&nbsp;│', '&nbsp;&nbsp;├─', '&nbsp;&nbsp;└─'];
        $tree->nbsp = '&nbsp;&nbsp;';
        foreach ($list as $key => $value) {

            $list[$key]['parent_id_node'] = ($value['parent_id']) ? ' class="child-of-node-' . $value['parent_id'] . '"' : '';
            $list[$key]['style']          = empty($value['parent_id']) ? '' : 'display:none;';

            $list[$key]['status'] = $value['status'] ? '<span class="label label-success">启用</span>' : '<span class="label label-danger">禁用</span>';;
            $list[$key]['str_manage'] = $value['status']  ? '<a class="js-ajax-dialog-btn btn btn-xs btn-warning" data-msg="确定要禁用吗？" href="'
                . url("AdminCategory/setStatusOff", ["id" => $value['id']]) . '">禁用</a> ' : '<a class="js-ajax-dialog-btn btn btn-xs btn-warning" data-msg="确定要启用吗？" href="'
                . url("AdminCategory/setStatusOn", ["id" => $value['id']]) . '">启用</a> ';
            $list[$key]['str_manage'] .= '<a class="btn btn-xs btn-primary" href="' . url("AdminCategory/add", ["parent" => $value['id']]) . '">添加子分类</a> ';
            $list[$key]['str_manage'] .= '<a class="btn btn-xs btn-primary" href="' . url("AdminCategory/edit", ["id" => $value['id']]) . '">编辑</a> ';
            $list[$key]['str_manage'] .= '<a class="js-ajax-dialog-btn btn btn-xs btn-danger" data-msg="确定要删除？" href="' . url("AdminCategory/delete", ["id" => $value['id']]) . '">删除</a>';
        }
        $tree->init($list);

        $tpl = "<tr id='node-\$id' \$parent_id_node style='\$style'>
            <td style='padding-left:20px;'><input name='list_orders[\$id]' type='text' size='3' value='\$list_order' class='input-order'></td>
            <td>\$id</td>
            <td>\$spacer \$name</td>
            <td>\$status</td>
            <td>\$str_manage</td>
        </tr>";

        $treeStr = $tree->getTree(0, $tpl);
		return $treeStr;
	}

	    /**
     * 生成分类 select树形结构
     * @param int $selectId   需要选中的分类 id
     * @param int $currentCid 需要隐藏的分类 id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function tenantCategoryTree($selectId = 0, $currentCid = 0)
    {
        $categories = $this->order("list_order ASC")
            ->where('delete_time', 0)
            ->where(function (Query $query) use ($currentCid) {
                if (!empty($currentCid)) {
                    $query->where('id', 'neq', $currentCid);
                }
            })
            ->select()->toArray();
        $tree       = new Tree();
        $tree->icon = ['&nbsp;&nbsp;│', '&nbsp;&nbsp;├─', '&nbsp;&nbsp;└─'];
        $tree->nbsp = '&nbsp;&nbsp;';
        $newCategories = [];
        foreach ($categories as $item) {
            $item['selected'] = $selectId == $item['id'] ? "selected" : "";

            array_push($newCategories, $item);
        }
        $tree->init($newCategories);
        $str     = '<option value=\"{$id}\" {$selected}>{$spacer}{$name}</option>';
        $treeStr = $tree->getTree(0, $str);

        return $treeStr;
    }

	/**
     * 子分类
     * @return Array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
	public function getChildren($parentId)
	{
		$list = $this->where(['status'=>1,'parent_id'=>$parentId])->order('list_order asc ,id asc')->select();
		return $list;
	}

	    /**
     * 添加分类
     * @param $data
     * @return bool
     * @throws \think\exception\PDOException
     */
    public function addCategory($data)
    {
        $result = true;
        self::startTrans();
        try {
        	$this->allowField(true)->save($data);
        	$id = $this->id;
            self::commit();
        } catch (\Exception $e) {
            self::rollback();
            $result = false;
        }

        return $result;
    }

    /**
     * 编辑分类
     * @param $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editCategory($data)
    {
        $result = true;

        $id          = intval($data['id']);
        $oldCategory = $this->where('id', $id)->find();

        if (empty($oldCategory)) {
            $result = false;
        } else {

            $this->isUpdate(true)->allowField(true)->save($data, ['id' => $id]);

        }
        return $result;
    }
}